﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

namespace SharingCenter.Web.Classes.Comments
{
    public class CommentOperations
    {
        public static List<Sc_CommentThread> GetThreads(string appName, string contextName, Guid contextId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                var q = from t in cxt.Sc_CommentThreads
                        where t.ContextName == contextName && t.ContextId == contextId && t.AppName == appName
                        select t;
                return q.ToList();
            }
        }

        public static List<Sc_Comment> GetCommentsInThread(Guid threadId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                var q = from t in cxt.Sc_Comments
                        where t.ThreadId == threadId && t.Approved
                        orderby t.CommentDate descending
                        select t;
                return q.ToList();
            }
        }

        public static Guid CreateThread(string appName, string contextName, Guid contextId, Guid userId, string content)
        {
            Sc_CommentThread s = new Sc_CommentThread();
            s.ContextName = contextName;
            s.AppName = appName;
            s.ContextId = contextId;
            s.CreateDate = DateTime.Now;
            s.ThreadId = Guid.NewGuid();
            s.CreatorUserId = userId;
            scDataContext.Instance.Sc_CommentThreads.InsertOnSubmit(s);
            AddCommentInThread(s.ThreadId, userId, content, false);

            try
            {
                scDataContext.Instance.SubmitChanges(ConflictMode.FailOnFirstConflict);
                return s.ThreadId;
            }
            catch { return Guid.Empty; }
            
        }

        public static void AddCommentInThread(Guid threadId, Guid userId, string content, bool submitChanges)
        {
            Sc_Comment cmt = new Sc_Comment();
            cmt.ThreadId = threadId;
            cmt.Approved = true;
            cmt.CommentContent = content;
            cmt.CommentDate = DateTime.Now;
            cmt.CommentId = Guid.NewGuid();
            cmt.ComposerUserId = userId;
            scDataContext.Instance.Sc_Comments.InsertOnSubmit(cmt);

            if (submitChanges)
            {
                scDataContext.Instance.SubmitChanges();
            }
        }

        public static void DeleteAllCommentsInThread(List<Guid> threadIds)
        {
            var q = from t in scDataContext.Instance.Sc_Comments
                    where threadIds.Contains(t.ThreadId)
                    select t;
            scDataContext.Instance.Sc_Comments.DeleteAllOnSubmit(q);

        }

        public static void DeleteAllThreadsInContext(string appName, string contextName, Guid contextId, bool submitChanges)
        {
            var q = GetThreads(appName, contextName, contextId);
            var l = from t in q 
                    select t.ThreadId;
            DeleteAllCommentsInThread(l.ToList());
            scDataContext.Instance.Sc_CommentThreads.DeleteAllOnSubmit(q);

            if (submitChanges)
            {
                scDataContext.Instance.SubmitChanges();
            }
        }
    }
}
